<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 报表API
 * @author zxj
 *
 */
class Report extends CI_Controller {
	function __construct() {
		parent::__construct();
        //$this->output->enable_profiler(TRUE);
	}

    /**
     * 报表面板
     */
    function index(){
        //get post data
        $formData = gp();
        //多语言
        $lang = $formData['lang'];
        $langArr = array('en','ch','es');
        if(!in_array($lang,$langArr)){
            $lang = 'ch';
        }
        $point = strtoupper($formData['point']);
        $points = $this->common_model->selectFirst('mt_service_center',array('point'=>$point));
        //查询条件
        $where = ' where b.available !=0 ';
        $merchants = $formData['seller'];
        if($merchants){
            $merchants = str_replace('-',',',$merchants);
            $where .= ' and b.merchant_num in('.$merchants.') ';
        }
        if($points){
            $where .= ' and b.service_center = '.$points['id'].' ';
        }
        //订单统计
        $orderStatus = $this->common_model->selectCustom('mt_order_status');
        foreach ($orderStatus as $rs) {
            $orderStatusArr[$rs['id']] = $rs['name_'.$lang];
        }
        $sql = 'select t.status_id,count(t.status_id)as cnt from (select b.status_id from mt_product a
        left join mt_order b on a.oid = b.id'.$where.'
        group by a.oid ORDER BY a.add_time desc)t GROUP BY t.status_id';
        $orderReport = $this->common_model->getQuery($sql,'result_array');
        $orderReportNew = array();
        foreach ($orderReport as $rs) {
            $rs['status_name'] = $orderStatusArr[$rs['status_id']];
            $orderReportNew[$rs['status_id']] = $rs;
        }
        //产品统计
        $productStatus = $this->common_model->selectCustom('mt_product_status');
        foreach ($productStatus as $rs) {
            $productStatusArr[$rs['id']] = $rs['name_'.$lang];
        }
        $sql = 'select a.status_id,COUNT(a.status_id) as cnt from mt_product a left join mt_order b on a.oid = b.id '.$where.' GROUP BY a.status_id';
        $productReport = $this->common_model->getQuery($sql,'result_array');
        $productReportNew = array();
        foreach ($productReport as $rs) {
            $rs['status_name'] = $productStatusArr[$rs['status_id']];
            $productReportNew[$rs['status_id']] = $rs;
        }
        //Hold统计
        $where .= ' and (a.available = 2 or b.available = 2) ';
        $sql = 'select b.id from mt_product as a left join mt_order as b on a.oid = b.id '.$where.' GROUP BY a.oid ';
        $holds = $this->common_model->getQuery($sql);
        $orderReportNew['-1'] = array('cnt'=>"$holds",'status_id'=>'-1','status_name'=>'Hold');
        //outPut
        $data = array('orderReport'=>$orderReportNew,'productReport'=>$productReportNew);
        if($formData['debug']){
            gsh_output($sql,1);
        }
        json_output($data, 'successfully', 1);
    }



}
